/*
 * Copyright (C) 2013 Fabiano Tarlao
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.easybenchmark.client.benchmarks;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.easybenchmark.client.compatibilityutils.GWTCollections;

/**
 * Benchmark object initialization as well as huge list creation and deletion
 * @author fab
 */
public class CollectionSortBenchmark extends Benchmark {
    private Random randGen = new Random();
    private int benchmarkSize = DEFAULT_WARMUP_BENCHMARK_SIZE;
    static private int DEFAULT_WARMUP_BENCHMARK_SIZE=50;
    
    public CollectionSortBenchmark() {
        super();
        setBenchmarkDescrition("Test collection sort and shuffle");
    }
    
    /**
     * The benchamark is done on array of given benchmarkWeight size
     * @param benchmarkWeight the size of test array
     */
    public CollectionSortBenchmark(int benchmarkWeight) {
        this();
        this.benchmarkSize=benchmarkWeight;
    }
        
    @Override
    protected long benchTask() {
        
       Collections.sort(testList);
       GWTCollections.shuffle(testList); 
        //return testList related result in order to avoid agressive optimizzations
        return(testList.size());
    }

    List<Integer> testList = new LinkedList<Integer>();
    
    @Override
    protected int initBenchTask() {
        initRandomList(benchmarkSize);
        return(testList.size());
    }

    private void initRandomList(int size){
        for (int i = 0; i < size; i++) {
         testList.add(randGen.nextInt());    
        }
    }
    
    
    @Override
    protected int initWarmUpTask() {
        initRandomList(DEFAULT_WARMUP_BENCHMARK_SIZE);
        return(testList.size());
    }
    
}
